package com.arlen.general.exceptionHandle;

import com.arlen.general.util.MessageThread;
import com.arlen.general.util.WxMessageUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @Author Arlen
 * @Description  全局异常捕获类
 * @Date 11:59 上午 2021/4/17
        **/
@ControllerAdvice(basePackages = "com.arlen.general")
public class CaptureException {


    @ExceptionHandler(WxCustomException.class)
    public void captureExcept(HttpServletResponse response, WxCustomException wxCustomException, HttpServletRequest request){
        response.setContentType("application/json;charset=utf-8");
        String message = wxCustomException.getMessage();
        //Map<String, String> mesMap = WxMessageUtil.analysisMessage(request);
        Map<String, String> mesMap = MessageThread.getMessageThread();
        String requestMes = "<xml>\n" +
                "  <ToUserName><![CDATA["+mesMap.get("FromUserName")+"]]></ToUserName>\n" +
                "  <FromUserName><![CDATA["+mesMap.get("ToUserName")+"]]></FromUserName>\n" +
                "  <CreateTime>"+System.currentTimeMillis()/1000+"</CreateTime>\n" +
                "  <MsgType><![CDATA[text]]></MsgType>\n" +
                "  <Content><![CDATA["+message+"]]></Content>\n" +
                "</xml>";
        try(PrintWriter writer = response.getWriter();){
                writer.write(requestMes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
